﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BLL;
using System.Data.SqlClient;

namespace DiChanAPI.Controllers
{
    public class RoleController : ApiController
    {
        // GET api/role
        [HttpGet]
        [ActionName("GetRole")]
        public IEnumerable<Roles> GetRole(string name)
        {

            if (name == null || name == "")
            {
                return new RolesBLL().GetListRole();
            }
            else
            {
                return new RolesBLL().GetListRole().Where(n => n.Roles_Name.Contains(name));
            }
        }
        [HttpGet]
        [ActionName("GetRoleByID")]
        // GET api/role/5
        public IEnumerable<Roles> GetRoleByID(int id) 
        {
            return new RolesBLL().GetListRole().Where(n=>n.Roles_Id==id);
        }
        [HttpPost]
        [ActionName("PostRole")]
        // POST api/role
        public bool PostRole([FromBody]Roles role) 
        {
            SqlParameter[] parame ={
                                      new SqlParameter("@Roles_Name",role.Roles_Name),
                                      new SqlParameter("@Roles_Remarks",role.Roles_Remarks),
                                      new SqlParameter("@Roles_IsState",role.Roles_IsState),
                                      new SqlParameter("@Roles_CreateTime",role.Roles_CreateTime),
                                      new SqlParameter("@Roles_CreatePeople",role.Roles_CreatePeople),
                                  };
            return new RolesBLL().AddRole(parame);
        }

        // PUT api/role/5
        [HttpPut]
        [ActionName("PutRole")]
        public bool PutRole([FromBody]Roles role)
        {
            SqlParameter[] parame ={
                                      new SqlParameter("@Roles_Id",role.Roles_Id),
                                      new SqlParameter("@Roles_Name",role.Roles_Name),
                                      new SqlParameter("@Roles_Remarks",role.Roles_Remarks),
                                      new SqlParameter("@Roles_IsState",role.Roles_IsState),
                                      new SqlParameter("@Roles_CreateTime",role.Roles_CreateTime),
                                      new SqlParameter("@Roles_CreatePeople",role.Roles_CreatePeople),
                                  };
            return new RolesBLL().UpdateRole(parame);
        }

        // DELETE api/role/5
        [HttpDelete]
        [ActionName("DeleteRole")]
        public bool DeleteRole(int id)
        {
            Roles role = new Roles();
            role.Roles_Id = id;
            return new RolesBLL().DeleteRole(role); 
        }
    }
}
